home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
TPUG - Toronto PET Users Group
/
TPUG Users Group CD
/
TPUG Users Group CD.iso
/
AMIGA
/
AMICUS
/
AMICUS08.ADF
/
C
/
Crunch.c
< prev
next >
Wrap
C/C++ Source or Header
|
1986-04-02
|
5KB
|
156 lines
/**********************************************************
* Crunch.c *
* by Larry Phillips. *
* *
* USAGE: CRUNCH INFILE [TO] OUTFILE *
* NOTE: Nothing fancy, use only above syntax *
* *
* Remove comments and unnecessary linefeeds from a *
* C source file. *
* *
* CREATED - 26 Dec. 85 *
* *
**********************************************************/
#define none 0
#define lf 10
#define space 32
#define comment 1
#define TRUE 1
#define FALSE 0
#include <stdio.h>
#include <ctype.h>
main(argc, argv)
int argc;
char *argv[];
{
int keepgoing = 0;
int flag = 0;
char c;
FILE *in, *out;
if (argc < 3)
{ puts("Bad arguments.\n");
exit(22);
}
if ((in = fopen (argv [1], "r")) == 0)
{ puts("Can't open inout file.\n");
exit(27);
}
if (argc == 4)
{
if ((out = fopen (argv [3], "w")) == 0)
{ puts("Can't open output file.\n");
exit(32);
}
}
if (argc == 3)
{
if ((out = fopen (argv [2], "w")) == 0)
{ puts("Can't open output file.\n");
exit(32);
}
}
while((c = getc(in)) !=EOF)
{
keepgoing = TRUE;
while (keepgoing == TRUE) /* till character resolved */
{
if (flag == none) /* check for special cases */
{
if (c == '/') /* check for possible comment */
{
if ((c = getc(in)) == '*') /* is comment, set flag */
{
flag = comment;
c = 0;
keepgoing = FALSE;
}
else /* not comment, output '/' */
putc ('/', out); /* leave c as new character */
} /* new character will be */
/* run through again */
if (c == '\n' || c == '\r') /* set linefeed flag */
flag = lf;
if (c == ' ' || c == '\t') /* set space flag on space or tab */
flag = space;
if (flag == none) /* no flags to be set. drop out */
keepgoing = FALSE; /* and put character to output */
} /* endif (flag = none) */
/*************************************************************************/
if (flag == space) /* if space flag on... */
{
if (c == ' ' || c == '\t') /* check for space or tab */
{
c = 0; /* yes... don't output */
keepgoing = FALSE;
}
else
{
putc (' ', out); /* no... output space and leave */
flag = none; /* c as new character to be run */
} /* through again. */
}
if (flag == lf)
{
if (c == '\n' || c == ' ' || c == '\r') /* strip extra */
{ /* linefeeds, tabs or */
c = 0; /* spaces after a '\n' */
keepgoing = FALSE;
}
else
{
putc ('\n', out); /* output 1 linefeed when printing char */
flag = none; /* is received. c saves new char */
}
}
if (flag == comment) /* process comments */
{
if (c == '*')
{
if ((c = getc(in)) == '/') /* if end of comment */
{
flag = none; /* turn off comment flag */
c = 0; /* we don't have next character */
keepgoing = FALSE; /* so go get next one */
}
}
else
{
c = 0;
keepgoing = FALSE;
}
}
} /* endwhile keepgoing */
if (c != 0)
putc (c, out);
} /* endwhile NOT EOF */
putc('\n', out);
fclose (in);
fclose (out);
} /* end of program */